#include <stdio.h>
#include "esp_log.h"
#include "i2c_custom.h"

static const char *TAG = "i2c_hal";

const i2c_config_t i2c_conf_func(void)
{
    const i2c_config_t i2c_conf = {
        .mode = I2C_MODE_MASTER,
        .sda_io_num = I2C_MASTER_SDA_IO,
        .scl_io_num = I2C_MASTER_SCL_IO,
        .sda_pullup_en = GPIO_PULLUP_ENABLE,
        .scl_pullup_en = GPIO_PULLUP_ENABLE,
        .master.clk_speed = I2C_MASTER_FREQ_HZ,
    };

    return i2c_conf;
}

const i2c_config_t ch423_i2c_conf_func(void)
{
    const i2c_config_t i2c_conf = {
        .mode = I2C_MODE_MASTER,
        .sda_io_num = CH423_I2C_MASTER_SDA_IO,
        .scl_io_num = CH423_I2C_MASTER_SCL_IO,
        .sda_pullup_en = GPIO_PULLUP_ENABLE,
        .scl_pullup_en = GPIO_PULLUP_ENABLE,
        .master.clk_speed = CH423_I2C_MASTER_FREQ_HZ,
    };

    return i2c_conf;
}
void i2c_init(void)
{
    ESP_LOGI(TAG, "Initialize I2C");
    /* Initialize I2C */
    ESP_LOGI(TAG, "Initialize I2C_0");
    const i2c_config_t i2c_conf = i2c_conf_func();
    ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &i2c_conf));
    ESP_ERROR_CHECK(i2c_driver_install(I2C_MASTER_NUM, i2c_conf.mode, 0, 0, 0));
    ESP_LOGI(TAG, "Initialize I2C_1");
    const i2c_config_t ch423_i2c_conf = ch423_i2c_conf_func();
    ESP_ERROR_CHECK(i2c_param_config(CH423_I2C_MASTER_NUM, &ch423_i2c_conf));
    ESP_ERROR_CHECK(i2c_driver_install(CH423_I2C_MASTER_NUM, ch423_i2c_conf.mode, 0, 0, 0));
}
